<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">


<meta name="google-site-verification" content="E9deYnivN5MuHMuIfiMZZfS0alv-d_0UjcwjBL79lGU" />



<meta name="baidu-site-verification" content="iHYWJxscwD" />










<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />



  <meta name="google-site-verification" content="true" />








  <meta name="baidu-site-verification" content="true" />







  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Python,量化投资,编程难题,MIT,编码与解码,游戏," />










<meta name="description" content="《programming for the puzzled》第三章本章涉及的内容：从用户获取输入，流程控制，编码和解码信息。问题描述：你是一个魔术师和会读心术的特异功能者。你在屋外时，五位观众从52张一副的扑克里每人选了一张，你的助手收集了这五张牌。助手向所有观众展示其中的4张牌，每次一张。展示每张牌的时候，助手让观众关注在牌上，而你尝试读取他们心里所想的。过几秒后，助手向你展示这张牌。四张牌都展示">
<meta property="og:type" content="article">
<meta property="og:title" content="量化投资学习笔记48——通过问题学算法:03你可以读心(you can read minds)">
<meta property="og:url" content="https://zwdnet.github.io/2020/05/05/%E9%87%8F%E5%8C%96%E6%8A%95%E8%B5%84%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B048%E2%80%94%E2%80%94%E9%80%9A%E8%BF%87%E9%97%AE%E9%A2%98%E5%AD%A6%E7%AE%97%E6%B3%95-03%E4%BD%A0%E5%8F%AF%E4%BB%A5%E8%AF%BB%E5%BF%83-you-can-read-minds/index.html">
<meta property="og:site_name" content="赵瑜敏的口腔医学专业学习博客">
<meta property="og:description" content="《programming for the puzzled》第三章本章涉及的内容：从用户获取输入，流程控制，编码和解码信息。问题描述：你是一个魔术师和会读心术的特异功能者。你在屋外时，五位观众从52张一副的扑克里每人选了一张，你的助手收集了这五张牌。助手向所有观众展示其中的4张牌，每次一张。展示每张牌的时候，助手让观众关注在牌上，而你尝试读取他们心里所想的。过几秒后，助手向你展示这张牌。四张牌都展示">
<meta property="og:locale">
<meta property="og:image" content="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/blog0178-QTLearn/36/01.png">
<meta property="og:image" content="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/blog0178-QTLearn/36/02.png">
<meta property="og:image" content="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/other/wx.jpg">
<meta property="article:published_time" content="2020-05-05T04:59:13.000Z">
<meta property="article:modified_time" content="2020-08-30T05:52:04.000Z">
<meta property="article:author" content="赵瑜敏">
<meta property="article:tag" content="Python">
<meta property="article:tag" content="量化投资">
<meta property="article:tag" content="编程难题">
<meta property="article:tag" content="MIT">
<meta property="article:tag" content="编码与解码">
<meta property="article:tag" content="游戏">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/blog0178-QTLearn/36/01.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://zwdnet.github.io/2020/05/05/量化投资学习笔记48——通过问题学算法-03你可以读心-you-can-read-minds/"/>





  <title>量化投资学习笔记48——通过问题学算法:03你可以读心(you can read minds) | 赵瑜敏的口腔医学专业学习博客</title>
  








<meta name="generator" content="Hexo 5.4.0"></head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">赵瑜敏的口腔医学专业学习博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/%20" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/%20" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/%20" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/%20" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://zwdnet.github.io/2020/05/05/%E9%87%8F%E5%8C%96%E6%8A%95%E8%B5%84%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B048%E2%80%94%E2%80%94%E9%80%9A%E8%BF%87%E9%97%AE%E9%A2%98%E5%AD%A6%E7%AE%97%E6%B3%95-03%E4%BD%A0%E5%8F%AF%E4%BB%A5%E8%AF%BB%E5%BF%83-you-can-read-minds/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/other/tx.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="赵瑜敏的口腔医学专业学习博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">量化投资学习笔记48——通过问题学算法:03你可以读心(you can read minds)</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2020-05-05T04:59:13+00:00">
                2020-05-05
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E9%87%8F%E5%8C%96%E6%8A%95%E8%B5%84/" itemprop="url" rel="index">
                    <span itemprop="name">量化投资</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                  <span class="post-meta-divider">|</span>
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  2.1k
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  9
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>《programming for the puzzled》第三章<br>本章涉及的内容：从用户获取输入，流程控制，编码和解码信息。<br>问题描述：你是一个魔术师和会读心术的特异功能者。你在屋外时，五位观众从52张一副的扑克里每人选了一张，你的助手收集了这五张牌。助手向所有观众展示其中的4张牌，每次一张。展示每张牌的时候，助手让观众关注在牌上，而你尝试读取他们心里所想的。过几秒后，助手向你展示这张牌。四张牌都展示完后，你离开屋子。助手向所有人展示第五张牌，并藏起来。你回来，并准确说出第五张牌是什么。原因是助手展示四张牌的顺序向你透露了第五张牌是什么。助手（而不是观众）决定展示哪四张牌，藏哪一张牌。<br>一个例子：假设观众选择了红心10，方块9，红心3，黑桃Q，方块J。<br>助手先把符号相同的两张牌检出来，五张牌至少有两张是符号相同的。在这个例子中是红心3和红心10.<br>助手把这两张牌放在如图所示的位置上。<br><img src="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/blog0178-QTLearn/36/01.png"><br>任意两张牌之间顺时针的距离在1-6之间。<br>展示其中一张牌，另一张仍被隐藏。被展示的那张牌是顺时针能在6步或更少的步数到达另一张牌的那张牌。在这个例子里，红心10到红心3顺时针需要5步，因此红心10先被展示。<br>未被展示的那张牌与第一个被展示的那张牌是一个符号。其位置与第一个被展示的牌的顺时针距离在1-6之间。<br>所有剩下的牌被赋予一个1-6的数值。魔术师和助手事先确认桌上的牌按不同花色从小到大排列。（梅花A方块A红心A黑桃A梅花2方块2红心2黑桃2…梅花K方块K红心K黑桃K）<br>根据最后三张牌来赋值，规则为：<br>(小、中、大） =  1<br>(小、大、中） =  2<br>(中、小、大） =  3<br>(中、大、小） =  4<br>(大、小、中） =  5<br>(大、中、小） =  6<br>助手想传达的数值是6，于是助手展示剩下三张牌的顺序是黑桃Q、方块J、方块9(大中小）。于是魔术师知道从红心10开始，顺时针走六张牌，就是隐藏的红心3了。<br>总结一下，就是随机摸5张牌，花色相同的两张展示一张，隐藏另一张。就知道隐藏的牌的花色，再用另外三张进行排列，传递花色相同的两张牌的位置信息，就可以确定隐藏的那张牌了。<br>还是我自己先写，写了好长时间，主要是调试，总有bug。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># coding:utf-8</span></span><br><span class="line"><span class="comment"># 《programming for the puzzled》实操</span></span><br><span class="line"><span class="comment"># 3.参加聚会的最佳时间</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">from</span> random <span class="keyword">import</span> sample</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 助手操作，展示</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">AssistantOrdersCards</span>(<span class="params">deck</span>):</span></span><br><span class="line">    choice = chooseCards(deck)</span><br><span class="line">    firstResults = findFirst(choice)</span><br><span class="line">    nextResults = nextCards(deck, choice, firstResults)</span><br><span class="line">    firstCard = restoreData([choice[firstResults[<span class="number">0</span>]]])</span><br><span class="line">    hideCard = restoreData([choice[firstResults[<span class="number">1</span>]]])</span><br><span class="line">    displayCards = (deck[nextResults[<span class="number">0</span>]], deck[nextResults[<span class="number">1</span>]], deck[nextResults[<span class="number">2</span>]])</span><br><span class="line">    print(<span class="string">&quot;隐藏的牌为:&quot;</span>, hideCard)</span><br><span class="line">    print(<span class="string">&quot;展示的四张牌分别为:&quot;</span>, firstCard[<span class="number">0</span>], displayCards[<span class="number">0</span>], displayCards[<span class="number">1</span>], displayCards[<span class="number">2</span>])</span><br><span class="line">    <span class="keyword">return</span> (firstCard[<span class="number">0</span>], displayCards[<span class="number">0</span>], displayCards[<span class="number">1</span>], displayCards[<span class="number">2</span>])</span><br><span class="line">   </span><br><span class="line">   </span><br><span class="line"><span class="comment"># 工具函数，选取五张牌，并进行数据转换</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">chooseCards</span>(<span class="params">deck</span>):</span></span><br><span class="line">    <span class="comment"># 随机抽五张牌</span></span><br><span class="line">    choice = sample(deck, <span class="number">5</span>)</span><br><span class="line">    choice = transformData(choice)</span><br><span class="line">    <span class="comment"># 使返回的数据固定，调试用的</span></span><br><span class="line">    <span class="comment"># choice = [[3, &#x27;D&#x27;], [4, &#x27;H&#x27;], [6, &#x27;C&#x27;], [5, &#x27;C&#x27;], [11, &#x27;H&#x27;]]</span></span><br><span class="line">    <span class="keyword">return</span> choice</span><br><span class="line">   </span><br><span class="line">   </span><br><span class="line"><span class="comment"># 工具函数 将原始数据转换为更容易计算的形式</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">transformData</span>(<span class="params">cards</span>):</span></span><br><span class="line">    <span class="comment"># 分割数据</span></span><br><span class="line">    choice = [[s.split(<span class="string">&quot;_&quot;</span>)[<span class="number">0</span>], s.split(<span class="string">&quot;_&quot;</span>)[<span class="number">1</span>]] <span class="keyword">for</span> s <span class="keyword">in</span> cards]</span><br><span class="line">    <span class="comment"># 数据转换，将第一个数据转换为数字</span></span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="built_in">len</span>(choice)):</span><br><span class="line">        <span class="keyword">if</span> choice[i][<span class="number">0</span>] == <span class="string">&quot;A&quot;</span>:</span><br><span class="line">            choice[i][<span class="number">0</span>] = <span class="number">1</span></span><br><span class="line">        <span class="keyword">elif</span> choice[i][<span class="number">0</span>] == <span class="string">&quot;J&quot;</span>:</span><br><span class="line">            choice[i][<span class="number">0</span>] = <span class="number">11</span></span><br><span class="line">        <span class="keyword">elif</span> choice[i][<span class="number">0</span>] == <span class="string">&quot;Q&quot;</span>:</span><br><span class="line">            choice[i][<span class="number">0</span>] = <span class="number">12</span></span><br><span class="line">        <span class="keyword">elif</span> choice[i][<span class="number">0</span>] == <span class="string">&quot;K&quot;</span>:</span><br><span class="line">            choice[i][<span class="number">0</span>] = <span class="number">13</span></span><br><span class="line">        <span class="keyword">else</span>:     <span class="comment">#已经是数字的了</span></span><br><span class="line">            choice[i][<span class="number">0</span>] = <span class="built_in">int</span>(choice[i][<span class="number">0</span>])</span><br><span class="line">    <span class="keyword">return</span> choice</span><br><span class="line">   </span><br><span class="line">   </span><br><span class="line"><span class="comment"># 工具函数，找出符号相同的两张牌，以及展示和隐藏的牌</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">findFirst</span>(<span class="params">cards</span>):</span></span><br><span class="line">    symbol = &#123;<span class="string">&#x27;C&#x27;</span>:<span class="number">0</span>, <span class="string">&#x27;D&#x27;</span>:<span class="number">0</span>, <span class="string">&#x27;H&#x27;</span>:<span class="number">0</span>, <span class="string">&#x27;S&#x27;</span>:<span class="number">0</span>&#125;</span><br><span class="line">    hide_card = <span class="string">&#x27;&#x27;</span></span><br><span class="line">    <span class="keyword">for</span> card <span class="keyword">in</span> cards:</span><br><span class="line">        symbol[card[<span class="number">1</span>]] += <span class="number">1</span></span><br><span class="line">    key = <span class="built_in">max</span>(symbol.keys(),key=(<span class="keyword">lambda</span> x:symbol[x]))</span><br><span class="line">    first = second = -<span class="number">1</span></span><br><span class="line">    i = <span class="number">0</span></span><br><span class="line">    <span class="keyword">for</span> card <span class="keyword">in</span> cards:</span><br><span class="line">        <span class="keyword">if</span> card[<span class="number">1</span>] == key:</span><br><span class="line">            <span class="keyword">if</span> first == -<span class="number">1</span>:</span><br><span class="line">                first = i</span><br><span class="line">            <span class="keyword">elif</span> second == -<span class="number">1</span>:</span><br><span class="line">                second = i</span><br><span class="line">                <span class="keyword">break</span></span><br><span class="line">        i += <span class="number">1</span></span><br><span class="line">       </span><br><span class="line">    <span class="comment"># 找出展示的牌和隐藏的牌。</span></span><br><span class="line">    v_first = cards[first][<span class="number">0</span>]</span><br><span class="line">    v_second = cards[second][<span class="number">0</span>]</span><br><span class="line">    gap = <span class="number">0</span></span><br><span class="line">    <span class="keyword">if</span> v_first &lt; v_second:</span><br><span class="line">        gap = v_second - v_first</span><br><span class="line">        <span class="keyword">if</span> gap &gt; <span class="number">6</span>:</span><br><span class="line">            first, second = second, first</span><br><span class="line">            gap = <span class="number">13</span> - gap</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        gap = v_first - v_second</span><br><span class="line">        <span class="keyword">if</span> gap &gt; <span class="number">6</span>:</span><br><span class="line">            gap = <span class="number">13</span> - gap</span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            first, second = second, first</span><br><span class="line">    <span class="keyword">return</span> (first, second, gap)</span><br><span class="line">   </span><br><span class="line">   </span><br><span class="line"><span class="comment"># 根据前两张牌的情况展示另外三张牌</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">nextCards</span>(<span class="params">deck, cards, firstResults</span>):</span></span><br><span class="line">    nextThree = []</span><br><span class="line">    i = <span class="number">0</span></span><br><span class="line">    <span class="keyword">for</span> card <span class="keyword">in</span> cards:</span><br><span class="line">        <span class="keyword">if</span> i != firstResults[<span class="number">0</span>] <span class="keyword">and</span> i != firstResults[<span class="number">1</span>]:</span><br><span class="line">            nextThree.append(card)</span><br><span class="line">        i += <span class="number">1</span></span><br><span class="line">    <span class="comment"># 将数据恢复成原始的形式</span></span><br><span class="line">    nextThree = restoreData(nextThree)</span><br><span class="line">    index = []</span><br><span class="line">    <span class="comment"># 得到最后三张牌在原数据中的位置，用于区分大中小</span></span><br><span class="line">    <span class="keyword">for</span> item <span class="keyword">in</span> nextThree:</span><br><span class="line">        index.append(deck.index(item))</span><br><span class="line">    result = displayOrder(index, firstResults[<span class="number">2</span>])</span><br><span class="line">    <span class="keyword">return</span> result</span><br><span class="line">   </span><br><span class="line">   </span><br><span class="line"><span class="comment"># 根据gap值计算最后三张牌的展示顺序</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">displayOrder</span>(<span class="params">index, gap</span>):</span></span><br><span class="line">    index = <span class="built_in">sorted</span>(index)</span><br><span class="line">    small, mid, large = index[<span class="number">0</span>], index[<span class="number">1</span>], index[<span class="number">2</span>]</span><br><span class="line">    <span class="comment"># 计算最后三张牌的展示顺序</span></span><br><span class="line">    result = []</span><br><span class="line">    <span class="keyword">if</span> gap == <span class="number">1</span>: <span class="comment"># 小中大</span></span><br><span class="line">        result = [small, mid, large]</span><br><span class="line">    <span class="keyword">elif</span> gap == <span class="number">2</span>: <span class="comment"># 小大中</span></span><br><span class="line">        result = [small, large, mid]</span><br><span class="line">    <span class="keyword">elif</span> gap == <span class="number">3</span>: <span class="comment"># 中小大</span></span><br><span class="line">        result = [mid, small, large]</span><br><span class="line">    <span class="keyword">elif</span> gap == <span class="number">4</span>: <span class="comment"># 中大小</span></span><br><span class="line">        result = [mid, large, small]</span><br><span class="line">    <span class="keyword">elif</span> gap == <span class="number">5</span>: <span class="comment"># 大小中</span></span><br><span class="line">        result = [large, small, mid]</span><br><span class="line">    <span class="keyword">elif</span> gap == <span class="number">6</span>: <span class="comment"># 大中小</span></span><br><span class="line">        result = [large, mid, small]</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        print(<span class="string">&quot;出错了 b&quot;</span>)</span><br><span class="line">   </span><br><span class="line">    <span class="keyword">return</span> result</span><br><span class="line">   </span><br><span class="line">   </span><br><span class="line"><span class="comment"># 工具函数，将数据还原会原来的样子。</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">restoreData</span>(<span class="params">cards</span>):</span></span><br><span class="line">    n = <span class="built_in">len</span>(cards)</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(n):</span><br><span class="line">        <span class="keyword">if</span> cards[i][<span class="number">0</span>] == <span class="number">1</span>:</span><br><span class="line">            cards[i][<span class="number">0</span>] = <span class="string">&#x27;A&#x27;</span></span><br><span class="line">        <span class="keyword">elif</span> cards[i][<span class="number">0</span>] == <span class="number">11</span>:</span><br><span class="line">            cards[i][<span class="number">0</span>] = <span class="string">&#x27;J&#x27;</span></span><br><span class="line">        <span class="keyword">elif</span> cards[i][<span class="number">0</span>] == <span class="number">12</span>:</span><br><span class="line">            cards[i][<span class="number">0</span>] = <span class="string">&#x27;Q&#x27;</span></span><br><span class="line">        <span class="keyword">elif</span> cards[i][<span class="number">0</span>] == <span class="number">13</span>:</span><br><span class="line">            cards[i][<span class="number">0</span>] = <span class="string">&#x27;K&#x27;</span></span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            cards[i][<span class="number">0</span>] = <span class="built_in">str</span>(cards[i][<span class="number">0</span>])</span><br><span class="line">    result = []</span><br><span class="line">    <span class="keyword">for</span> card <span class="keyword">in</span> cards:</span><br><span class="line">        s = <span class="string">&#x27;_&#x27;</span>.join(card)</span><br><span class="line">        result.append(s)</span><br><span class="line">    <span class="keyword">return</span> result</span><br><span class="line">   </span><br><span class="line">   </span><br><span class="line"><span class="comment"># 魔术师根据cards里展示的四张牌猜牌</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">MagicianGuessesCard</span>(<span class="params">deck, cards</span>):</span></span><br><span class="line">    new_cards = [card <span class="keyword">for</span> card <span class="keyword">in</span> cards]</span><br><span class="line">    new_cards = transformData(new_cards)</span><br><span class="line">    <span class="comment"># 先确定牌的符号，跟第一张牌一样</span></span><br><span class="line">    symble = new_cards[<span class="number">0</span>][<span class="number">1</span>]</span><br><span class="line">    <span class="comment"># 下面确定牌的数值</span></span><br><span class="line">    gap = getGap(deck, cards)</span><br><span class="line">    <span class="comment"># 计算隐藏的牌的数值</span></span><br><span class="line">    num = new_cards[<span class="number">0</span>][<span class="number">0</span>] + gap</span><br><span class="line">    <span class="keyword">if</span> num &gt; <span class="number">13</span>:</span><br><span class="line">        num = num - <span class="number">13</span></span><br><span class="line">    <span class="comment"># 数值和符号都有了，可以合并得到结果了。</span></span><br><span class="line">    result = [[num, symble]]</span><br><span class="line">    result = restoreData(result)</span><br><span class="line">    <span class="keyword">return</span> result</span><br><span class="line">   </span><br><span class="line">   </span><br><span class="line"><span class="comment"># 工具函数，根据牌的顺序确定前两张牌的间隔</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">getGap</span>(<span class="params">deck, cards</span>):</span></span><br><span class="line">    index = []</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">1</span>, <span class="number">4</span>):</span><br><span class="line">        index.append(deck.index(cards[i]))</span><br><span class="line">    new_index = <span class="built_in">sorted</span>(index)</span><br><span class="line">    small, mid, large =new_index[<span class="number">0</span>], new_index[<span class="number">1</span>], new_index[<span class="number">2</span>]</span><br><span class="line">    <span class="comment"># 根据三张牌排列情况计算gap值</span></span><br><span class="line">    <span class="keyword">if</span> index == [small, mid, large]: <span class="comment"># 小中大</span></span><br><span class="line">        gap = <span class="number">1</span></span><br><span class="line">    <span class="keyword">elif</span> index == [small, large, mid]: <span class="comment"># 小大中</span></span><br><span class="line">        gap = <span class="number">2</span></span><br><span class="line">    <span class="keyword">elif</span> index == [mid, small, large]: <span class="comment"># 中小大</span></span><br><span class="line">        gap = <span class="number">3</span></span><br><span class="line">    <span class="keyword">elif</span> index == [mid, large, small]: <span class="comment"># 中大小</span></span><br><span class="line">        gap = <span class="number">4</span></span><br><span class="line">    <span class="keyword">elif</span> index == [large, small, mid]: <span class="comment"># 大小中</span></span><br><span class="line">        gap = <span class="number">5</span></span><br><span class="line">    <span class="keyword">elif</span> index == [large, mid, small]: <span class="comment"># 大中小</span></span><br><span class="line">        gap = <span class="number">6</span></span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        print(<span class="string">&quot;出错了 a&quot;</span>)</span><br><span class="line">    <span class="keyword">return</span> gap</span><br><span class="line">   </span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&quot;__main__&quot;</span>:</span><br><span class="line">    deck = [<span class="string">&#x27;A_C&#x27;</span>,<span class="string">&#x27;A_D&#x27;</span>,<span class="string">&#x27;A_H&#x27;</span>,<span class="string">&#x27;A_S&#x27;</span>,</span><br><span class="line">                  <span class="string">&#x27;2_C&#x27;</span>,<span class="string">&#x27;2_D&#x27;</span>,<span class="string">&#x27;2_H&#x27;</span>,<span class="string">&#x27;2_S&#x27;</span>,</span><br><span class="line">                  <span class="string">&#x27;3_C&#x27;</span>,<span class="string">&#x27;3_D&#x27;</span>,<span class="string">&#x27;3_H&#x27;</span>,<span class="string">&#x27;3_S&#x27;</span>,</span><br><span class="line">                  <span class="string">&#x27;4_C&#x27;</span>,<span class="string">&#x27;4_D&#x27;</span>,<span class="string">&#x27;4_H&#x27;</span>,<span class="string">&#x27;4_S&#x27;</span>,</span><br><span class="line">                  <span class="string">&#x27;5_C&#x27;</span>,<span class="string">&#x27;5_D&#x27;</span>,<span class="string">&#x27;5_H&#x27;</span>,<span class="string">&#x27;5_S&#x27;</span>,</span><br><span class="line">                  <span class="string">&#x27;6_C&#x27;</span>,<span class="string">&#x27;6_D&#x27;</span>,<span class="string">&#x27;6_H&#x27;</span>,<span class="string">&#x27;6_S&#x27;</span>,</span><br><span class="line">                  <span class="string">&#x27;7_C&#x27;</span>,<span class="string">&#x27;7_D&#x27;</span>,<span class="string">&#x27;7_H&#x27;</span>,<span class="string">&#x27;7_S&#x27;</span>,</span><br><span class="line">                  <span class="string">&#x27;8_C&#x27;</span>,<span class="string">&#x27;8_D&#x27;</span>,<span class="string">&#x27;8_H&#x27;</span>,<span class="string">&#x27;8_S&#x27;</span>,</span><br><span class="line">                  <span class="string">&#x27;9_C&#x27;</span>,<span class="string">&#x27;9_D&#x27;</span>,<span class="string">&#x27;9_H&#x27;</span>,<span class="string">&#x27;9_S&#x27;</span>,</span><br><span class="line">                  <span class="string">&#x27;10_C&#x27;</span>,<span class="string">&#x27;10_D&#x27;</span>,<span class="string">&#x27;10_H&#x27;</span>,<span class="string">&#x27;10_S&#x27;</span>,</span><br><span class="line">                  <span class="string">&#x27;J_C&#x27;</span>,<span class="string">&#x27;J_D&#x27;</span>,<span class="string">&#x27;J_H&#x27;</span>,<span class="string">&#x27;J_S&#x27;</span>,</span><br><span class="line">                  <span class="string">&#x27;Q_C&#x27;</span>,<span class="string">&#x27;Q_D&#x27;</span>,<span class="string">&#x27;Q_H&#x27;</span>,<span class="string">&#x27;Q_S&#x27;</span>,</span><br><span class="line">                  <span class="string">&#x27;K_C&#x27;</span>,<span class="string">&#x27;K_D&#x27;</span>,<span class="string">&#x27;K_H&#x27;</span>,<span class="string">&#x27;K_S&#x27;</span>]</span><br><span class="line">    cards = AssistantOrdersCards(deck)</span><br><span class="line">    result = MagicianGuessesCard(deck, cards)</span><br><span class="line">    print(<span class="string">&quot;猜牌结果为:&quot;</span>, result)</span><br></pre></td></tr></table></figure>
<p><img src="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/blog0178-QTLearn/36/02.png"><br>终于对了。但是很繁琐，有200多行。再看看作者写的，思路跟我差不多，也是将近200行。就不搬过来了。这种程序，算法不难，调试了好长时间，很多地方都是加一减一的错误。<br>一个更难的问题，只选四张牌能不能玩这个游戏？因为可能四张牌符号都不同，一个解决方法是把52张牌都放到那个圈里，一定有两张牌小于等于13。具体不再实现了。<br>练习也pass了，实在不喜欢这个题目。视频课程里现场玩了这个游戏。<br>本章代码： <a target="_blank" rel="noopener" href="https://github.com/zwdnet/MyQuant/blob/master/44/03">https://github.com/zwdnet/MyQuant/blob/master/44/03</a></p>
<p>我发文章的三个地方，欢迎大家在朋友圈等地方分享，欢迎点“在看”。<br>我的个人博客地址：<a href="https://zwdnet.github.io/">https://zwdnet.github.io</a><br>我的知乎文章地址： <a target="_blank" rel="noopener" href="https://www.zhihu.com/people/zhao-you-min/posts">https://www.zhihu.com/people/zhao-you-min/posts</a><br>我的微信个人订阅号：赵瑜敏的口腔医学学习园地</p>
<p><img src="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/other/wx.jpg"></p>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>欢迎打赏！感谢支持！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/other/mm_facetoface_collect_qrcode_1542944836634.png" alt=" 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/other/1542944857770.jpg" alt=" 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Python/" rel="tag"># Python</a>
          
            <a href="/tags/%E9%87%8F%E5%8C%96%E6%8A%95%E8%B5%84/" rel="tag"># 量化投资</a>
          
            <a href="/tags/%E7%BC%96%E7%A8%8B%E9%9A%BE%E9%A2%98/" rel="tag"># 编程难题</a>
          
            <a href="/tags/MIT/" rel="tag"># MIT</a>
          
            <a href="/tags/%E7%BC%96%E7%A0%81%E4%B8%8E%E8%A7%A3%E7%A0%81/" rel="tag"># 编码与解码</a>
          
            <a href="/tags/%E6%B8%B8%E6%88%8F/" rel="tag"># 游戏</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2020/05/05/%E7%89%99%E9%AB%93%E7%97%85%E6%80%BB%E7%BB%9317%E2%80%94%E2%80%94%E6%A0%B9%E7%AE%A1%E6%B2%BB%E7%96%97%E7%9A%84%E9%A2%84%E5%90%8E/" rel="next" title="牙髓病总结17——根管治疗的预后">
                <i class="fa fa-chevron-left"></i> 牙髓病总结17——根管治疗的预后
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2020/05/06/%E5%AD%99%E5%AE%87%E8%80%81%E5%B8%88%E3%80%8A%E4%B8%AA%E4%BA%BA%E5%AD%A6%E4%B9%A0%E6%88%90%E9%95%BF%E4%B9%8B%E6%97%85%E3%80%8B%E5%AD%A6%E4%B9%A0%E8%AE%B0%E5%BD%95/" rel="prev" title="孙宇老师《个人学习成长之旅》学习记录">
                孙宇老师《个人学习成长之旅》学习记录 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="lv-container" data-id="city" data-uid="MTAyMC80MTA2Mi8xNzU4Nw=="></div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/other/tx.jpg"
                alt="" />
            
              <p class="site-author-name" itemprop="name"></p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/%20%7C%7C%20archive">
              
                  <span class="site-state-item-count">452</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">29</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">544</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">本站版权归赵瑜敏所有，如欲转载请与本人联系。</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    
      <span class="post-meta-item-text">Site words total count&#58;</span>
    
    <span title="Site words total count">1225.8k</span>
  
</div>









<div>
  <script type="text/javascript">var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://");document.write(unescape("%3Cspan id='cnzz_stat_icon_1275447216'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s11.cnzz.com/z_stat.php%3Fid%3D1275447216%26online%3D1%26show%3Dline' type='text/javascript'%3E%3C/script%3E"));</script>
</div>

        







  <div style="display: none;">
    <script src="//s95.cnzz.com/z_stat.php?id=1275447216&web_id=1275447216" language="JavaScript"></script>
  </div>



        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  
    <script type="text/javascript">
      (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
      })(document, 'script');
    </script>
  












  





  

  

  

  
  

  

  

  

  
</body>
</html>
